home *** CD-ROM | disk | FTP | other *** search
- Path: mudskipper.cac.psu.edu!user
- From: fcusack@tdx.org (frank.)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: Sat, 03 Feb 1996 16:45:28 -0400
- Organization: Penn State University, Center for Academic Computing
- Message-ID: <fcusack-0302961645280001@mudskipper.cac.psu.edu>
- References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net> <fcusack-0202961621470001@mudskipper.cac.psu.edu>
- NNTP-Posting-Host: mudskipper.cac.psu.edu
-
- In article <fcusack-0202961621470001@mudskipper.cac.psu.edu>,
- fcusack@tdx.org (frank.) wrote:
-
- > In article <4etj7c$bma@news.iag.net>, jatmon@iag.net (John R Buchan) wrote:
- >
- > > In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
- > > >
- > > >
- > > >#include <stdio.h>
- > > >#include <stdlib.h>
- > > >#include <string.h>
- > > >
- > > >int main (int argc, char **argv) {
- > > >
- > > > void Usage (void);
- > > > void Encode (int, char **);
- > > > void Decode (int, char **);
- > > >
- > > > if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
- > > > Usage();
- > > > }
- > > <snip>
- > >
- > > I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
- > > Change the || to &&.
- >
-
- I mistakenly wrote:
-
- > The && test would _never_ pass. argv[1] could never be both "-d" and "-e".
- > The || is correct here.
- >
-
- Yes, the && test would never pass, but the || test is bad also. The ||
- will pass if you pass "-d" or "-e". So then you will call Usage()
- incorrectly. The test should be:
-
- if (!(!strcmp(argv[1], "-d") || !strcmp(argv[1], "-e"))) {
- Usage();
- }
-
- > >Of course, if no argument was passed, who knows what
- > > will happen here. You should always test argc first to be certain that
- > > the argv element exists.
- >
-
- and I also incorrectly wrote:
-
- > argv[1] is always guaranteed to exist. It is simply NULL if there were no
- > arguments.
-
- Dan Pop has the poop on this one in his followup. eg, on the Mac, argc is
- probably always zero, thus argv[1] is undefined.
- ~Frank
- -- I am Pentium of Borg. Division is futile. You will be approximated. --
- -- If you build it, they will come --> http://www.tdx.org/~fcusack/ --
- -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F 3F 64 80 65 8B 0F F9 EA --
-